﻿<?php
class Cts_Db_Table extends Zend_Db_Table_Abstract
{
    protected $_name ;
    protected $_select ;
        
    public function insert( $aData )
    {
        parent::insert( $aData ) ;
		return $this->_db->lastInsertId( $this->_name ) ;    
    }
    
    public function getList( $strLocator , $nStart = NULL , $nLimit = NULL )
    {
        $this->_select = $this->_db->select();
    	$this->_select->limit ( $nLimit , $nStart ) ;
    	$this->_select->from( $this->_name, '*' ) 
			          ->where( $strLocator )
    	              ->order( 'id asc' ) ;
        $sql = $this->_select->__toString( ) ;
        //echo $sql ;
        $result = $this->_db->fetchAll( $sql ) ;
        return $result ; 
    }
    
    public function getById( $nId )
    {
        $objColTable = new Yinte_Db_ColArr() ;
        $this->_select = $this->_db->select();
        $this->_select->from( $this->_name, '*' )
                      ->where( 'id = ?', $nId ) ;
        $sql = $this->_select->__toString() ;
        $result = $this->_db->fetchRow( $sql ) ;
        return $result ;    
    }
    
    public function getCount( $strLocator = NULL )
    {
        if( !isset( $strLocator ) || empty( $strLocator ) )
        {
            $strLocator = '1=1' ;
        }
        $this->_select = $this->_db->select();
    	$this->_select->from( $this->_name, 'count(*) as count' )
			          ->where( $strLocator ) ;
        $sql = $this->_select->__toString() ;
        $total = $this->_db->fetchCol( $sql ) ;
        return $total[0] ;    
    }
    
    public function analyseLocator( $aLocator )
    {
        if( is_array( $aLocator ) )
        {
            $nCount = 0 ;
            foreach( $aLocator as $key => $val )
            {
                $aTemp = explode( '_' , $key , 2 ) ;
                switch( $aTemp[0] )
                {
                    case 'locate' :
                        $strTempLocator = $this->getAdapter()->quoteIdentifier( $aTemp[1] ) . ' = ' . $this->getAdapter()->quote( $val ) ;
                        break ;
                    case 'inrange'    :
                    case 'notinrange' :
                        switch( $aTemp[0] )
                        {
                            case 'inrange' :
                                $strOperator = " IN " ;
                                break ;
                            case 'notinrange' :
                                $strOperator = " NOT IN " ;
                                break ;
                        }
                        //$aVal = explode( ',' , $val ) ;
                        $nCountValue = 0 ;
                        foreach( $val as $k => $v )
                        {
                            if( $nCountValue > 0 )
                            {
                                $strQuoteValue .= ' , ' . $this->getAdapter()->quote( $v ) ;
                            }
                            else
                            {
                                $strQuoteValue .= $this->getAdapter()->quote( $v ) ;    
                            }
                            $nCountValue++ ;
                        }
                        $strTempLocator = $this->getAdapter()->quoteIdentifier( $aTemp[1] ) . $strOperator . '( ' . $strQuoteValue . ' ) ' ;
                        break ;
                    case 'search' :
                        $strTempLocator = $this->getAdapter()->quoteIdentifier( $aTemp[1] ) . ' LIKE BINARY \'%' . $val . '%\' ' ;
                        break ;
                    case 'largethan' :
                    case 'largeequal':
                    case 'lessthan'  :
                    case 'lessequal' :
                    case 'notequal'  :
                        switch( $aTemp[0] )    
                        {
                            case "largethan":
								$strOperator = " > ";
								break;
							case "largeequal":
								$strOperator = " >= ";
								break;
							case "lessthan":
								$strOperator = " < ";
								break;
							case "lessequal":
								$strOperator = " <= ";
								break;
							case "notequal":
							    $strOperator = " != ";
								break;
                        }
                        $strTempLocator = $this->getAdapter()->quoteIdentifier( $aTemp[1] ) . $strOperator . $this->getAdapter()->quote( $val ) ;
                        break ;
                    case 'between' :
                        $strTempLocator = $this->getAdapter()->quoteIdentifier( $aTemp[1] ) . ' BETWEEN ' . $this->getAdapter()->quote( $val[0] ) . ' AND ' . $this->getAdapter()->quote( $val[1] ) ;
                        break ;
                    default :
                        break ;   
                }
                if( $nCount > 0 )
                {
                    $strLocator .= ' AND ' . $strTempLocator ;    
                }
                else
                {
                    $strLocator = $strTempLocator ;       
                }
                $nCount++ ;
            }
        }   
        else
        {
            $strLocator = $aLocator ;    
        } 
        return $strLocator ;
    }
}
